package ua.org.extjsbuilder.parser.runtime;

import java.io.IOException;
import java.io.Reader;

import ua.org.extjsbuilder.parser.IRuntimeBuilder;
import ua.org.extjsbuilder.parser.IScope;
import ua.org.extjsbuilder.parser.IToken;
import ua.org.extjsbuilder.parser.ITokenList;
import ua.org.extjsbuilder.parser.ITokenScanner;
import ua.org.extjsbuilder.parser.JSParseException;

public class JSGlobalToken extends GeneralScope 
		implements IScope, ITokenList, IRuntimeBuilder {
	public JSGlobalToken(Reader reader) throws IOException{
		super(null,null);
		head = new InitialToken(
			new UnparsedToken(reader));
	}

	public int getLine() {
		return 0;
	}

	public IToken getHead() {
		return head;
	}

	public IToken getNext() {
		return null;
	}
	
	@Override
	protected UnparsedToken getStartToken() {
		return getStartToken(head.getNext());
	}

	public void scan(ITokenScanner scanner) {
		scanner.init();
		IToken h = head;
		while(h!=null){
			h.scan(scanner);
			h=h.getNext();
		}
		scanner.finish();
	}

	public void split(IToken... replacement) {
		throw new JSParseException("You cannot split global token",0);
	}
	
	private static class InitialToken extends AbstractToken {
		protected InitialToken(AbstractToken next) {
			super(new char[0],0);
			this.next = next;
			next.previous = this;
		}
	}
}
